﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace QuanLyNhaHang.DataAccess
{
    class DAOrder
    {
        DataAccess.SqlService service;

        public DAOrder()
        {
            service = new SqlService();
        }

        public DataTable loadListView(String check)
        {
            String StoreName = null,paramItem = null;
            String value = "";

            if (check == "dsKhuVuc")
            {
                paramItem = "@TEN_KHUVUC";
                StoreName = "ad_KHUVUC_GetAll";
            }
            else if (check == "dsBan")
            {
                paramItem = "@TENBAN";
                StoreName = "ad_DSBAN_GetAll";
            }
            else if (check == "dsDMTD")
            {
                paramItem = "@TENDMUC";
                StoreName = "ad_DMTDON_GetAll";
            }
            else if (check == "dsTD")
            {
                paramItem = "@IDTD";
                StoreName = "ad_THUCDON_GetAll";
            }
            //Load via getDataTable.
            SqlParameter[] param = {new SqlParameter(paramItem,value)};
            DataTable dt2 = service.getDTStoredProcedure(StoreName, param);
            return dt2;
        }

        public String getLocationForNewTable(String table)
        {
            SqlParameter[] param = {new SqlParameter("@TENBAN",table)};
            return (String)service.executeScalarSProcedure("ad_DSBAN_GetTENKV_WhereTENBAN", param);
        }

        #region New Table LeftTable in Order
        public DataTable optionNTLoadCbo()
        {
            SqlParameter[] param = { new SqlParameter("@TEN_KHUVUC", "") };
            DataTable dt = new DataTable();
            dt = service.getDTStoredProcedure("ad_KHUVUC_GetAll", param);
            return dt;
        }

        public bool optionCheckNewTable(String[] a)
        {
            String sqlCount = "SELECT COUNT(*) FROM DS_BAN WHERE TENBAN = N'" + a[1] + "'";
            if (a[1] != null)
            {
                int i = (int)service.executeScalar(sqlCount);
                if (i > 0)
                {
                    return true;
                }
            }
            
            if (a[1] == "")
            {
                return true;
            }

            return false;
        }

        public bool optionNTNew(String[] a)
        {
            try
            {
                SqlParameter[] param = { new SqlParameter("@OPTION", "INSERT"), new SqlParameter("@TENBAN", a[1]), new SqlParameter("@TENKV", a[0]) };
                service.ExcuteStoredProcedure("ad_DSBAN_Edit", param);
            }
            catch (Exception)
            {
                return false;
            }
            return true; 
        }

        #endregion

        public bool deleteTable(String table)
        {
            try
            {
                SqlParameter[] param = { new SqlParameter("@OPTION", "DELETE"), new SqlParameter("@TENBAN", table), new SqlParameter("@TENKV", "") };
                service.ExcuteStoredProcedure("ad_DSBAN_Edit", param);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }


    }
}
